// 按键监听
document.onkeydown = function (event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    // Ctrl + Enter 切换引擎
    if(e.ctrlKey && e.keyCode === 13) {
        var engine = document.getElementById('engine').getElementsByTagName('li');
        helangSearch.searchIndex = 
            helangSearch.searchIndex ===
                helangSearch.hot.href.length - 1 ? 0 : helangSearch.searchIndex + 1;
        for(var i = 0; i < helangSearch.hot.href.length; i++) {
            if(i === helangSearch.searchIndex) {
                helangSearch.els.pickerBtn.html(engine[i].innerText);
            }
        }
        setEngineList(helangSearch);
    }
    // ESC 隐藏导航列表/获取焦点
    if(e.keyCode === 27) {
        index = -1;
        helangSearch.els.pickerList.hide();
        helangSearch.els.hotList.hide();
        document.getElementById("search-input").focus();
    }
    // ↑ ↓ 显示导航列表/选择导航
    if(e.keyCode === 38 || e.keyCode === 40) {
        var as = document.getElementById("hot-list").getElementsByTagName("a");
        helangSearch.els.hotList.show();
        if(e.keyCode === 38) {
            index = index > -1 ? index - 1 : -1;
        } else {
            index = index < as.length - 1 ? index + 1 : 0;
        }
        index === -1 ? document.getElementById("search-input").focus() : as[index].focus({preventScroll:false});
    }
    // Enter 调用翻译接口
    if(helangSearch.searchIndex === 1 && !e.ctrlKey && e.keyCode === 13 && index === -1) {
        setFanyiList();
    }
    // Enter 执行 JavaScript 脚本
    if(helangSearch.searchIndex === 2 && !e.ctrlKey && e.keyCode === 13 && index === -1) {
        setJsList();
    }
}